package com.bestcem.xm.user.service.impl;

import cn.hutool.core.lang.ObjectId;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.user.dao.mapper.OrganizationMapper;
import com.bestcem.xm.user.enums.OrgWalletConfigRechargeLimitEnum;
import com.bestcem.xm.user.service.OrganizationWalletConfigService;
import com.bestcem.xm.user.service.dto.organization.OrganizationWalletConfigDTO;
import com.bestcem.xm.user.service.init.UserDataInitService;
import com.bestcem.xm.user.util.business.DataBaseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 初始化数据service
 *
 * @author wangyunyun <yunyun.wang@idiaoyan.com>
 * @version v1.0
 * @date 2022/7/13 10:46
 */
@Slf4j
@Service
public class UserDataInitServiceImpl implements UserDataInitService {
    @Resource
    private OrganizationMapper organizationMapper;
    @Resource
    private OrganizationWalletConfigService organizationWalletConfigService;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public ServiceResult<String> orgWalletConfigInit() {
        List<String> orgIds = organizationMapper.listIdByOrgInfo(null, null);
        List<OrganizationWalletConfigDTO> walletConfigDTOList = new ArrayList<>();
        Date now = DataBaseUtil.getDate();
        for (String orgId : orgIds) {
            OrganizationWalletConfigDTO walletConfigDTO = new OrganizationWalletConfigDTO();
            walletConfigDTO.setWalletConfigId(ObjectId.next());
            walletConfigDTO.setOrgId(orgId);
            walletConfigDTO.setRechargeLimit(OrgWalletConfigRechargeLimitEnum.NO_LIMIT.getType());
            walletConfigDTO.setCreateTime(now);
            walletConfigDTO.setUpdateTime(now);

            walletConfigDTOList.add(walletConfigDTO);
        }
        organizationWalletConfigService.insertBatch(walletConfigDTOList);

        return ServiceResult.success();
    }
}
